﻿// ***********************************************************************
// Assembly         : Admin.Framework.Core.Infrastructure
// Author           : xwltz
// Created          : 2019-05-30
//
// Last Modified By : xwltz
// Last Modified On : 2019-05-30
// ***********************************************************************
// <summary>
//  加解密服务类
// </summary>
// ***********************************************************************

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;

// ReSharper disable All
namespace Admin.Framework.Core.Infrastructure.Helper
{
    public class CryptoService
    {
        #region MD5

        /// <summary>
        /// 将字符串转换为md5加密
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string Md5(string str)
        {
            string result;
            using (var mD5CryptoServiceProvider = new MD5CryptoServiceProvider())
            {
                result = BitConverter.ToString(mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "").ToLower();
            }
            return result;
        }

        /// <summary>
        /// Md5Hash
        /// </summary>
        /// <param name="encypStr"></param>
        /// <returns></returns>
        public static string Md5Hash(string encypStr)
        {
            var m5 = new MD5CryptoServiceProvider();
            var inputBye = Encoding.UTF8.GetBytes(encypStr);
            var outputBye = m5.ComputeHash(inputBye);
            var retStr = Convert.ToBase64String(outputBye);
            return (retStr);
        }

        /// <summary>
        /// 将字符串转换为md5加密 16位
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string Md5S16(string str)
        {
            return Md5S(str, 16);
        }

        /// <summary>
        /// 将字符串转换为md5加密 32位
        /// </summary>
        /// <param name="str">字符串</param>
        /// <returns></returns>
        public static string Md5S32(string str)
        {
            return Md5(str);
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">加密字符</param>
        /// <param name="code">加密位数16/32</param>
        /// <returns></returns>
        public static string Md5S(string str, int code = 32)
        {
            using (var mD5CryptoServiceProvider = new MD5CryptoServiceProvider())
            {
                var result = BitConverter.ToString(mD5CryptoServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "").ToLower();
                if (code != 16) return result;
                string value = null;
                for (var i = 0; i < result.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        value += result[i].ToString();
                    }
                }
                return value;
            }
        }

        /// <summary>
        /// MD5加密
        /// </summary>
        /// <param name="str">字符串</param>
        /// <param name="str16">16位还是32位，16位就是取32位的第8到16位</param>
        /// <returns></returns>
        public static string MD5Encrypt(string str, bool str16)
        {
            var result = Encoding.UTF8.GetBytes(str);
            var md5 = new MD5CryptoServiceProvider();
            var output = md5.ComputeHash(result);
            return str16 ? BitConverter.ToString(output).Replace("-", "").ToUpper().Substring(8, 16) : BitConverter.ToString(output).Replace("-", "").ToUpper();
        }

        /// <summary>
        /// HMACMD5加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="hmacKey"></param>
        /// <returns></returns>
        public static string HMACMD5Encrypt(string str, string hmacKey)
        {
            var hmacsha1 = new HMACMD5(Encoding.UTF8.GetBytes(hmacKey));
            var result = Encoding.UTF8.GetBytes(str);
            var output = hmacsha1.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToUpper();
        }

        #endregion MD5

        #region BASE64

        /// <summary>
        /// BASE64加密字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Base64Encode(string str)
        {
            return Convert.ToBase64String(Encoding.UTF8.GetBytes(str));
        }

        /// <summary>
        /// BASE64解密字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Base64Decode(string str)
        {
            return Encoding.UTF8.GetString(Convert.FromBase64String(str));
        }

        #endregion BASE64

        #region SHA OR HMACSHA

        /// <summary>
        /// 对字符串进行SHA1加密
        /// </summary>
        /// <param name="str">需要加密的字符串</param>
        /// <returns>密文</returns>
        public static string SHA1Encrypt(string str)
        {
            var strRes = Encoding.UTF8.GetBytes(str);
            HashAlgorithm iSha = new SHA1CryptoServiceProvider();
            strRes = iSha.ComputeHash(strRes);
            var enText = new StringBuilder();
            foreach (var iByte in strRes)
            {
                enText.AppendFormat("{0:x2}", iByte);
            }
            return enText.ToString().ToUpper();
        }

        /// <summary>
        /// 对字符串进行HMACSHA1加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="hmacKey"></param>
        /// <returns></returns>
        public static string HMACSHA1Encrypt(string str, string hmacKey)
        {
            var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(hmacKey));
            var result = Encoding.UTF8.GetBytes(str);
            var output = hmacsha1.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToUpper();
        }

        /// <summary>
        /// SHA256加密，不可逆转
        /// </summary>
        /// <param name="str">string str:被加密的字符串</param>
        /// <returns>返回加密后的字符串</returns>
        public static string SHA256Encrypt(string str)
        {
            var strRes = Encoding.UTF8.GetBytes(str);
            HashAlgorithm iSha = new SHA256CryptoServiceProvider();
            strRes = iSha.ComputeHash(strRes);
            var enText = new StringBuilder();
            foreach (var iByte in strRes)
            {
                enText.AppendFormat("{0:x2}", iByte);
            }
            return enText.ToString().ToUpper();
        }

        /// <summary>
        /// 对字符串进行HMACSHA256加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="hmacKey"></param>
        /// <returns></returns>
        public static string HMACSHA256Encrypt(string str, string hmacKey)
        {
            var hmacsha1 = new HMACSHA256(Encoding.UTF8.GetBytes(hmacKey));
            var result = Encoding.UTF8.GetBytes(str);
            var output = hmacsha1.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToUpper();
        }

        /// <summary>
        /// SHA384加密，不可逆转
        /// </summary>
        /// <param name="str">string str:被加密的字符串</param>
        /// <returns>返回加密后的字符串</returns>
        public static string SHA384Encrypt(string str)
        {
            var strRes = Encoding.UTF8.GetBytes(str);
            HashAlgorithm iSha = new SHA384CryptoServiceProvider();
            strRes = iSha.ComputeHash(strRes);
            var enText = new StringBuilder();
            foreach (var iByte in strRes)
            {
                enText.AppendFormat("{0:x2}", iByte);
            }
            return enText.ToString().ToUpper();
        }

        /// <summary>
        /// 对字符串进行HMACSHA384加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="hmacKey"></param>
        /// <returns></returns>
        public static string HMACSHA384Encrypt(string str, string hmacKey)
        {
            var hmacsha1 = new HMACSHA384(Encoding.UTF8.GetBytes(hmacKey));
            var result = Encoding.UTF8.GetBytes(str);
            var output = hmacsha1.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToUpper();
        }

        /// <summary>
        /// SHA512加密，不可逆转
        /// </summary>
        /// <param name="str">string str:被加密的字符串</param>
        /// <returns>返回加密后的字符串</returns>
        public static string SHA512Encrypt(string str)
        {
            var strRes = Encoding.UTF8.GetBytes(str);
            HashAlgorithm iSha = new SHA512CryptoServiceProvider();
            strRes = iSha.ComputeHash(strRes);
            var enText = new StringBuilder();
            foreach (var iByte in strRes)
            {
                enText.AppendFormat("{0:x2}", iByte);
            }
            return enText.ToString().ToUpper();
        }

        /// <summary>
        /// 对字符串进行HMACSHA512加密
        /// </summary>
        /// <param name="str"></param>
        /// <param name="hmacKey"></param>
        /// <returns></returns>
        public static string HMACSHA512Encrypt(string str, string hmacKey)
        {
            var hmacsha1 = new HMACSHA512(Encoding.UTF8.GetBytes(hmacKey));
            var result = Encoding.UTF8.GetBytes(str);
            var output = hmacsha1.ComputeHash(result);
            return BitConverter.ToString(output).Replace("-", "").ToUpper();
        }

        #endregion SHA OR HMACSHA

        #region DES

        /// <summary>
        /// DES加密
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="encryptKey">加密密匙，要求为8位</param>
        /// <returns></returns>
        public static string DESEncrypt(string encryptString, string encryptKey)
        {
            using (var des = new DESCryptoServiceProvider())
            {
                var inputByteArray = Encoding.UTF8.GetBytes(encryptString);
                des.Key = GetDESKey(encryptKey);
                des.IV = GetDESKey(encryptKey);
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cs.Close();
                    }

                    var ret = new StringBuilder();
                    foreach (var b in ms.ToArray())
                    {
                        ret.AppendFormat("{0:x2}", b);
                    }
                    return ret.ToString();
                }
            }
        }

        /// <summary>
        /// DES解密字符串
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="decryptKey">解密密钥,要求为8位,和加密密钥相同</param>
        /// <returns></returns>
        public static string DESDecrypt(string decryptString, string decryptKey)
        {
            using (var des = new DESCryptoServiceProvider())
            {
                var inputByteArray = new byte[decryptString.Length / 2];
                for (var x = 0; x < decryptString.Length / 2; x++)
                {
                    var i = Convert.ToInt32(decryptString.Substring(x * 2, 2), 16);
                    inputByteArray[x] = (byte)i;
                }

                des.Key = GetDESKey(decryptKey);
                des.IV = GetDESKey(decryptKey);
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        return Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                    }
                }
            }
        }

        #endregion DES

        #region AES

        /// <summary>
        /// AES加密 CBC模式
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AESEncryptIV(string str, string key)
        {
            try
            {
                //using (var aes = new AesCryptoServiceProvider())
                //{
                //    var inputByteArray = Encoding.UTF8.GetBytes(str);

                //    aes.Key = GetAESKey(key);
                //    aes.GenerateIV();

                //    using (var ms = new MemoryStream())
                //    {
                //        using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
                //        {
                //            cs.Write(inputByteArray, 0, inputByteArray.Length);
                //            cs.FlushFinalBlock();
                //            cs.Close();
                //        }

                //        var ret = new StringBuilder();
                //        foreach (var b in ms.ToArray())
                //        {
                //            ret.AppendFormat("{0:x2}", b);
                //        }
                //        return ret.ToString();
                //    }
                //}

                var rijndaelCipher = new RijndaelManaged();
                var inputByteArray = Encoding.UTF8.GetBytes(str);
                rijndaelCipher.Key = GetAESKey(key);
                rijndaelCipher.GenerateIV();
                var keyIv = rijndaelCipher.IV;
                byte[] cipherBytes;
                using (var ms = new MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, rijndaelCipher.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        cs.Write(inputByteArray, 0, inputByteArray.Length);
                        cs.FlushFinalBlock();
                        cipherBytes = ms.ToArray();
                        cs.Close();
                        ms.Close();
                    }
                }
                var allEncrypt = new byte[keyIv.Length + cipherBytes.Length];
                Buffer.BlockCopy(keyIv, 0, allEncrypt, 0, keyIv.Length);
                Buffer.BlockCopy(cipherBytes, 0, allEncrypt, keyIv.Length * sizeof(byte), cipherBytes.Length);
                return Convert.ToBase64String(allEncrypt);
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// AES解密 CBC模式
        /// </summary>
        /// <param name="str"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string AESDecryptIV(string str, string key)
        {
            try
            {
                //using (var aes = new AesCryptoServiceProvider())
                //{
                //    var inputByteArray = Encoding.UTF8.GetBytes(str);

                //    aes.Key = GetAESKey(key);
                //    aes.GenerateIV();

                //    using (var ms = new MemoryStream())
                //    {
                //        using (var cs = new CryptoStream(ms, aes.CreateDecryptor(), CryptoStreamMode.Write))
                //        {
                //            cs.Write(inputByteArray, 0, inputByteArray.Length);
                //            cs.FlushFinalBlock();
                //            return Encoding.UTF8.GetString(ms.ToArray());
                //        }
                //    }
                //}

                var cipherText = Convert.FromBase64String(str);
                var length = cipherText.Length;
                SymmetricAlgorithm rijndaelCipher = Rijndael.Create();
                rijndaelCipher.Key = GetAESKey(key);
                var iv = new byte[16];
                Buffer.BlockCopy(cipherText, 0, iv, 0, 16);
                rijndaelCipher.IV = iv;
                var decryptBytes = new byte[length - 16];
                var passwdText = new byte[length - 16];
                Buffer.BlockCopy(cipherText, 16, passwdText, 0, length - 16);
                using (var ms = new MemoryStream(passwdText))
                {
                    using (var cs = new CryptoStream(ms, rijndaelCipher.CreateDecryptor(), CryptoStreamMode.Read))
                    {
                        cs.Read(decryptBytes, 0, decryptBytes.Length);
                        cs.Close();
                        ms.Close();
                    }
                }
                return Encoding.UTF8.GetString(decryptBytes).Replace("\0", "");
            }
            catch
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// AES加密 ECB模式
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="key">aes密钥，长度必须32位</param>
        /// <returns>加密后的字符串</returns>
        public static string AESEncrypt(string source, string key)
        {
            try
            {
                using (var aesProvider = new AesCryptoServiceProvider())
                {
                    aesProvider.Key = GetAESKey(key);
                    aesProvider.Mode = CipherMode.ECB;
                    aesProvider.Padding = PaddingMode.PKCS7;
                    using (var cryptoTransform = aesProvider.CreateEncryptor())
                    {
                        var inputBuffers = Encoding.UTF8.GetBytes(source);
                        var results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
                        aesProvider.Clear();
                        aesProvider.Dispose();
                        return Convert.ToBase64String(results, 0, results.Length);
                    }
                }
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// AES解密 ECB模式
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="key">aes密钥，长度必须32位</param>
        /// <returns>解密后的字符串</returns>
        public static string AESDecrypt(string source, string key)
        {
            try
            {
                using (var aesProvider = new AesCryptoServiceProvider())
                {
                    aesProvider.Key = GetAESKey(key);
                    aesProvider.Mode = CipherMode.ECB;
                    aesProvider.Padding = PaddingMode.PKCS7;
                    using (var cryptoTransform = aesProvider.CreateDecryptor())
                    {
                        var inputBuffers = Convert.FromBase64String(source);
                        var results = cryptoTransform.TransformFinalBlock(inputBuffers, 0, inputBuffers.Length);
                        aesProvider.Clear();
                        return Encoding.UTF8.GetString(results);
                    }
                }
            }
            catch
            {
                return "";
            }
        }

        #endregion AES

        #region RSA

        //##############################################################################   
        //RSA 方式加密   
        //KEY必须是XML的形式,返回的是字符串   
        //该加密方式有长度限制，需要特别注意！  
        //##############################################################################   

        #region RSA产生密钥

        /// <summary>
        ///     RSA产生密钥
        /// </summary>
        /// <param name="xmlPrivateKey">私钥</param>
        /// <param name="xmlPublicKey">公钥</param>
        public void RSAKey(out string xmlPrivateKey, out string xmlPublicKey)
        {
            try
            {
                var rsa = new RSACryptoServiceProvider();
                xmlPrivateKey = rsa.ToXmlString(true);
                xmlPublicKey = rsa.ToXmlString(false);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion

        #region RSA加密函数  

        /// <summary>
        ///     RSA的加密函数
        /// </summary>
        /// <param name="xmlPublicKey">公钥</param>
        /// <param name="encryptString">待加密的字符串</param>
        /// <returns></returns>
        public static string RSAEncrypt(string xmlPublicKey, string encryptString)
        {
            try
            {
                var rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPublicKey);
                var plainTextBArray = new UnicodeEncoding().GetBytes(encryptString);
                var cypherTextBArray = rsa.Encrypt(plainTextBArray, false);
                var result = Convert.ToBase64String(cypherTextBArray);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        ///     RSA的加密函数
        /// </summary>
        /// <param name="xmlPublicKey">公钥</param>
        /// <param name="encryptString">待加密的字节数组</param>
        /// <returns></returns>
        public static string RSAEncrypt(string xmlPublicKey, byte[] encryptString)
        {
            try
            {
                var rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPublicKey);
                var cypherTextBArray = rsa.Encrypt(encryptString, false);
                var result = Convert.ToBase64String(cypherTextBArray);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// RSA对称加密
        /// </summary>
        /// <param name="encryptString">待加密的字符串</param>
        /// <param name="key">秘钥</param>
        /// <returns></returns>
        public static string RSAEnCode(string encryptString, string key)
        {
            var param = new CspParameters { KeyContainerName = key };
            using (var rsa = new RSACryptoServiceProvider(param))
            {
                var plaindata = Encoding.UTF8.GetBytes(encryptString);
                var encryptdata = rsa.Encrypt(plaindata, false);
                return Convert.ToBase64String(encryptdata);
            }
        }

        #endregion

        #region RSA解密函数

        /// <summary>
        ///     RSA的解密函数
        /// </summary>
        /// <param name="xmlPrivateKey">私钥</param>
        /// <param name="decryptString">待解密的字符串</param>
        /// <returns></returns>
        public static string RSADecrypt(string xmlPrivateKey, string decryptString)
        {
            try
            {
                byte[] PlainTextBArray;
                byte[] DypherTextBArray;
                string Result;
                var rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPrivateKey);
                PlainTextBArray = Convert.FromBase64String(decryptString);
                DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
                Result = new UnicodeEncoding().GetString(DypherTextBArray);
                return Result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        ///     RSA的解密函数
        /// </summary>
        /// <param name="xmlPrivateKey">私钥</param>
        /// <param name="decryptString">待解密的字节数组</param>
        /// <returns></returns>
        public static string RSADecrypt(string xmlPrivateKey, byte[] decryptString)
        {
            try
            {
                var rsa = new RSACryptoServiceProvider();
                rsa.FromXmlString(xmlPrivateKey);
                var dypherTextBArray = rsa.Decrypt(decryptString, false);
                var result = new UnicodeEncoding().GetString(dypherTextBArray);
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// RSA对称解密
        /// </summary>
        /// <param name="decryptString">待解密的字符串</param>
        /// <param name="key">秘钥</param>
        /// <returns></returns>
        public static string RSADeCode(string decryptString, string key)
        {
            var param = new CspParameters { KeyContainerName = key };
            using (var rsa = new RSACryptoServiceProvider(param))
            {
                var encryptdata = Encoding.UTF8.GetBytes(decryptString);
                var decryptdata = rsa.Decrypt(encryptdata, false);
                return Encoding.UTF8.GetString(decryptdata);
            }
        }

        #endregion

        #endregion RSA

        #region 得到随机安全码（哈希加密）

        /// <summary>
        /// 得到随机哈希加密字符串
        /// </summary>
        /// <returns></returns>
        public static string GetSecurity()
        {
            var security = HashEncoding(GetRandomValue());
            return security;
        }

        /// <summary>
        /// 得到一个随机数值
        /// </summary>
        /// <returns></returns>
        public static string GetRandomValue()
        {
            var seed = new Random();
            var randomVaule = seed.Next(1, int.MaxValue).ToString();
            return randomVaule;
        }

        /// <summary>
        /// 哈希加密一个字符串
        /// </summary>
        /// <param name="security"></param>
        /// <returns></returns>
        public static string HashEncoding(string security)
        {
            var code = new UnicodeEncoding();
            var message = code.GetBytes(security);
            var arithmetic = new SHA512Managed();
            var value = arithmetic.ComputeHash(message);
            return value.Aggregate("", (current, o) => current + ((int)o + "O"));
        }

        #endregion 得到随机安全码（哈希加密）

        #region Private Method

        /// <summary>
        /// 生成MD5摘要
        /// </summary>
        /// <param name="original">数据源</param>
        /// <returns>摘要</returns>
        public static byte[] MakeMd5(byte[] original)
        {
            var hashmd5 = new MD5CryptoServiceProvider();
            var keyhash = hashmd5.ComputeHash(original);
            return keyhash;
        }

        private static byte[] GetAESKey(string key)
        {
            if (key.Length < 32)
            {
                key = key.PadRight(32, '0');
            }
            if (key.Length > 32)
            {
                key = key.Substring(0, 32);
            }
            return Encoding.UTF8.GetBytes(key);
        }

        private static byte[] GetDESKey(string key)
        {
            if (key.Length < 8)
            {
                key = key.PadRight(8, '0');
            }
            if (key.Length > 8)
            {
                key = key.Substring(0, 8);
            }
            return Encoding.UTF8.GetBytes(key);
        }

        private static byte[] GetRSAKey(string key)
        {
            if (key.Length < 64)
            {
                key = key.PadRight(64, '0');
            }
            if (key.Length > 64)
            {
                key = key.Substring(64, 8);
            }
            return Encoding.UTF8.GetBytes(key);
        }

        #endregion Private Method

        #region 使用 缺省密钥字符串 加密/解密string

        /// <summary>
        /// 使用缺省密钥字符串加密string
        /// </summary>
        /// <param name="original">明文</param>
        /// <returns>密文</returns>
        public static string Encrypt(string original)
        {
            return Encrypt(original, FrameworkConfigs.Key);
        }

        /// <summary>
        /// 使用缺省密钥字符串解密string
        /// </summary>
        /// <param name="original">密文</param>
        /// <returns>明文</returns>
        public static string Decrypt(string original)
        {
            return Decrypt(original, FrameworkConfigs.Key, Encoding.UTF8);
        }

        #endregion 使用 缺省密钥字符串 加密/解密string

        #region 使用 给定密钥字符串 加密/解密string

        /// <summary>
        /// 使用给定密钥字符串加密string
        /// </summary>
        /// <param name="original">原始文字</param>
        /// <param name="key">密钥</param>
        /// <returns>密文</returns>
        public static string Encrypt(string original, string key)
        {
            var buff = Encoding.UTF8.GetBytes(original);
            var kb = Encoding.UTF8.GetBytes(key);
            return Convert.ToBase64String(Encrypt(buff, kb));
        }

        /// <summary>
        /// 使用给定密钥字符串解密string
        /// </summary>
        /// <param name="original">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static string Decrypt(string original, string key)
        {
            return Decrypt(original, key, Encoding.UTF8);
        }

        /// <summary>
        /// 使用给定密钥字符串解密string,返回指定编码方式明文
        /// </summary>
        /// <param name="encrypted">密文</param>
        /// <param name="key">密钥</param>
        /// <param name="encoding">字符编码方案</param>
        /// <returns>明文</returns>
        public static string Decrypt(string encrypted, string key, Encoding encoding)
        {
            var buff = Convert.FromBase64String(encrypted);
            var kb = Encoding.UTF8.GetBytes(key);
            return encoding.GetString(Decrypt(buff, kb));
        }

        #endregion 使用 给定密钥字符串 加密/解密string

        #region 使用 缺省密钥字符串 加密/解密/byte[]

        /// <summary>
        /// 使用缺省密钥字符串解密byte[]
        /// </summary>
        /// <param name="encrypted">密文</param>
        /// <returns>明文</returns>
        public static byte[] Decrypt(byte[] encrypted)
        {
            var key = Encoding.UTF8.GetBytes(FrameworkConfigs.Key);
            return Decrypt(encrypted, key);
        }

        /// <summary>
        /// 使用缺省密钥字符串加密
        /// </summary>
        /// <param name="original">原始数据</param>
        /// <returns>密文</returns>
        public static byte[] Encrypt(byte[] original)
        {
            var key = Encoding.UTF8.GetBytes(FrameworkConfigs.Key);
            return Encrypt(original, key);
        }

        #endregion 使用 缺省密钥字符串 加密/解密/byte[]

        #region 使用 给定密钥 加密/解密/byte[]

        /// <summary>
        /// 使用给定密钥加密
        /// </summary>
        /// <param name="original">明文</param>
        /// <param name="key">密钥</param>
        /// <returns>密文</returns>
        public static byte[] Encrypt(byte[] original, byte[] key)
        {
            var des = new TripleDESCryptoServiceProvider
            {
                Key = MakeMd5(key),
                Mode = CipherMode.ECB
            };

            return des.CreateEncryptor().TransformFinalBlock(original, 0, original.Length);
        }

        /// <summary>
        /// 使用给定密钥解密数据
        /// </summary>
        /// <param name="encrypted">密文</param>
        /// <param name="key">密钥</param>
        /// <returns>明文</returns>
        public static byte[] Decrypt(byte[] encrypted, byte[] key)
        {
            var des = new TripleDESCryptoServiceProvider
            {
                Key = MakeMd5(key),
                Mode = CipherMode.ECB
            };

            return des.CreateDecryptor().TransformFinalBlock(encrypted, 0, encrypted.Length);
        }

        #endregion 使用 给定密钥 加密/解密/byte[]

        #region 简单加密 解密

        //密钥
        private static readonly byte[] ArrDesKey = { 42, 16, 93, 156, 78, 4, 218, 32 };

        private static readonly byte[] ArrDesiv = { 55, 103, 246, 79, 36, 99, 167, 3 };

        /// <summary>
        /// 加密。
        /// </summary>
        /// <param name="mNeedEncodeString"></param>
        /// <returns></returns>
        public static string Encode(string mNeedEncodeString)
        {
            if (mNeedEncodeString == null)
            {
                throw new Exception("Error: \n源字符串为空！！");
            }
            var objDes = new DESCryptoServiceProvider();
            var objMemoryStream = new MemoryStream();
            var objCryptoStream = new CryptoStream(objMemoryStream, objDes.CreateEncryptor(ArrDesKey, ArrDesiv), CryptoStreamMode.Write);
            var objStreamWriter = new StreamWriter(objCryptoStream);
            objStreamWriter.Write(mNeedEncodeString);
            objStreamWriter.Flush();
            objCryptoStream.FlushFinalBlock();
            objMemoryStream.Flush();
            return Convert.ToBase64String(objMemoryStream.GetBuffer(), 0, (int)objMemoryStream.Length);
        }

        /// <summary>
        /// 解密。
        /// </summary>
        /// <param name="mNeedEncodeString"></param>
        /// <returns></returns>
        public static string Decode(string mNeedEncodeString)
        {
            if (mNeedEncodeString == null)
            {
                throw new Exception("Error: \n源字符串为空！！");
            }
            var objDes = new DESCryptoServiceProvider();
            var arrInput = Convert.FromBase64String(mNeedEncodeString);
            var objMemoryStream = new MemoryStream(arrInput);
            var objCryptoStream = new CryptoStream(objMemoryStream, objDes.CreateDecryptor(ArrDesKey, ArrDesiv), CryptoStreamMode.Read);
            var objStreamReader = new StreamReader(objCryptoStream);
            return objStreamReader.ReadToEnd();
        }

        #endregion 简单加密 解密
    }
}